package com.wmx.hb.controller;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wmx.hb.enums.ResultCode;
import com.wmx.hb.mapper.DeptMapper;
import com.wmx.hb.pojo.Dept;
import com.wmx.hb.pojo.ResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门控制层
 *
 * @author wangMaoXiong
 * @version 1.0
 * @date 2021/7/4 16:45
 */
@SuppressWarnings("all")
@RestController
public class DeptController {

    private static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Resource
    private DeptMapper deptMapper;

    /**
     * 添加部门信息：  http://localhost:8080/dept/add
     * {"dname":"Java开发部","loc":"长沙"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept/add")
    public ResultData<String> addDept(@RequestBody Dept dept) {
        Integer addDept = deptMapper.addDept(dept);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, "影响条数=" + addDept + ",主键 deptno=" + dept.getDeptno());
        return resultData;
    }

    /**
     * http://localhost:8080/dept/1
     * 根据主键查询
     *
     * @param deptno
     * @return
     */
    @GetMapping("/dept/{deptno}")
    public ResultData<Dept> findPartyById(@PathVariable("deptno") Integer deptno) {
        Dept dept = deptMapper.findDeptById(deptno);
        return new ResultData<>(ResultCode.SUCCESS, dept);
    }

    /**
     * http://localhost:8080/dept/findByLimit?page=2&rows=2
     * 分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/dept/findByLimit")
    public ResultData<List<Dept>> findByLimit(Integer page, Integer rows) {
        page = page == null || page <= 0L ? 1 : page;
        rows = rows == null || rows <= 0L ? 2 : rows;

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("start", (page - 1) * rows);
        paramMap.put("end", (page - 1) * rows + rows);
        paramMap.put("orderColumn", "dname");

        List<Dept> deptList = deptMapper.findByLimit(paramMap);
        return new ResultData<>(ResultCode.SUCCESS, deptList, deptMapper.countForTable(), page, rows);
    }

    /**
     * 查询表中的数据总条数
     * http://localhost:8080/dept/countForTable
     *
     * @return
     */
    @GetMapping("/dept/countForTable")
    public ResultData<Long> countForTable() {
        Long countForTable = deptMapper.countForTable();
        return new ResultData<>(ResultCode.SUCCESS, countForTable);
    }

    /**
     * 查询所有部门的名称
     * http://localhost:8080/dept/findAllNames
     *
     * @return
     */
    @GetMapping("/dept/findAllNames")
    public ResultData<List<String>> findAllNames() {
        List<String> allNames = deptMapper.findAllNames();
        return new ResultData<>(ResultCode.SUCCESS, allNames);
    }

    /**
     * http://localhost:8080/depts
     * 查询所有
     *
     * @return
     */
    @GetMapping("/depts")
    public ResultData findAllDepts() {
        List<Dept> allDepts = deptMapper.findAllDepts();
        ResultData resultData = new ResultData(allDepts);
        return resultData;
    }

    /**
     * 根据主键 id集合查询
     * http://localhost:8080/findByDeptnos
     * [1,2,3,4,5]
     *
     * @param deptnos
     * @return
     */
    @PostMapping("/findByDeptnos")
    public ResultData findByDeptnos(@RequestBody(required = false) List<Integer> deptnos) {
        deptnos = deptnos == null ? new ArrayList<>() : deptnos;
        List<Dept> deptList = deptMapper.findByDeptnos(deptnos);
        ResultData resultData = new ResultData(deptList);
        return resultData;
    }

    /**
     * 查询指定部门的信息，将实体对象中不为空的属性都作为 where 条件，默认查所有
     * http://localhost:8080/findByDept
     * http://localhost:8080/findByDept?pageNum=2&pageSize=3
     * {"deptno":5}
     * {"dname":"开发"}
     * {"dname":"开发","loc":"长沙市"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/findByDept")
    public PageInfo<Dept> findByDept(@RequestBody(required = false) Dept dept, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null || pageNum <= 0L ? 1 : pageNum;
        pageSize = pageSize == null || pageSize <= 0L ? 2 : pageSize;

        //此时必须配置 pagehelper.supportMethodsArguments=true，开启支持 Mapper 方法参数分页.
        List<Dept> deptList = deptMapper.findByDept(dept, pageNum, pageSize);
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);
        return pageInfo;
    }

    /**
     * 查询指定部门的信息，将实体对象中不为空的属性都作为 where 条件，默认查所有
     * http://localhost:8080/loadByDept
     * {"deptno":5}
     * {"dname":"开发"}
     * {"dname":"开发","loc":"深圳市"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/loadByDept")
    public ResultData loadByDept(@RequestBody(required = false) Dept dept) {
        List<Dept> deptList = deptMapper.loadByDept(dept);
        ResultData resultData = new ResultData(deptList);
        return resultData;
    }

    /**
     * 根据关键字模糊查询，默认查询所有
     * http://localhost:8080/findByKeyword?keyword=长沙
     * http://localhost:8080/findByKeyword?pageNum=2&pageSize=4
     *
     * @param keyword
     * @return
     */
    @GetMapping("/findByKeyword")
    public PageInfo<Dept> findByKeyword(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null || pageNum <= 0 ? 0 : pageNum;
        pageSize = pageSize == null || pageSize <= 0L ? 0 : pageSize;

        //doSelectPage 返回的是 Page，doSelectPageInfo 返回的是 PageInfo.
        PageInfo<Dept> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(new ISelect() {
            @Override
            public void doSelect() {
                deptMapper.findByKeyword(keyword);
            }
        });
        return pageInfo;
    }

    /**
     * 根据关键字模糊查询，默认查询所有。分页插件查询
     * http://localhost:8080/findByKeywordPage1
     * http://localhost:8080/findByKeywordPage1?keyword=长沙
     * http://localhost:8080/findByKeywordPage1?pageNum=2&pageSize=4
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByKeywordPage1")
    public PageInfo findByKeywordPage1(String keyword, Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null || pageNum <= 0 ? 0 : pageNum;
        pageSize = pageSize == null || pageSize <= 0L ? 0 : pageSize;
        /**
         * 1、PageHelper.startPage(int pageNum, int pageSize)：开始分页，此时 XxxMapper 返回的集合是 {@link com.github.pagehelper.Page} 分页对象
         * total(总记录数) 等于 ((Page)list).getTotal();
         * 2、PageHelper.offsetPage：开始分页，底层与 startPage 是一样的.
         * 3、如果 pageNum 或者 pageSize 为 0，则不分页，默认查询所有，此时 XxxMapper 返回的集合不再是 {@link com.github.pagehelper.Page} 分页对象
         * 此时 size(当前页的数量)、total(总记录数) 都是等于普通的 list.size()
         * 4、只有紧跟在 PageHelper.startPage、PageHelper.offsetPage 方法后的第一个 Mybatis 的查询(Select)方法会被分页，
         * 除非再次调用 PageHelper.startPage 或者调用 PageHelper.offsetPage
         */
        if (pageNum != 0 && pageSize != 0) {
            //PageHelper.startPage(pageNum, pageSize);
            PageHelper.offsetPage(pageNum, pageSize);
        }
        List<Dept> deptList = deptMapper.findByKeyword(keyword);

        /**
         * PageInfo(List<T> list)：包装 Page 对象，默认导航页码为8
         * PageInfo(List<T> list, int navigatePages)：包装 Page 对象，指定导航页码数量
         */
        PageInfo pageInfo = new PageInfo(deptList);
        return pageInfo;
    }

    /**
     * http://localhost:8080/dept/emp?deptno=10
     * 查询指定部门的信息，以及部门下的所有人员信息.
     *
     * @param deptno
     * @return
     */
    @GetMapping("dept/emp")
    public ResultData findDeptEmp(@RequestParam Integer deptno) {
        List<Map<String, Object>> deptEmp = deptMapper.findDeptEmp(deptno);
        ResultData resultData = new ResultData(deptEmp);
        return resultData;
    }

    /**
     * 修改：  http://localhost:8080/dept/update
     * {"deptno":6,"dname":"IOS开发部","loc":"长沙市"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept/update")
    public ResultData updateDept(@RequestBody Dept dept) {
        Integer integer = deptMapper.updateDept(dept);
        ResultData resultData = new ResultData(integer);
        return resultData;
    }

    /**
     * 修改：  http://localhost:8080/dept/updateDeptByNotNull
     * {"deptno":1,"dname":"IOS开发部","loc":"长沙市"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept/updateDeptByNotNull")
    public ResultData updateDeptByNotNull(@RequestBody Dept dept) {
        Integer integer = deptMapper.updateDeptByNotNull(dept);
        ResultData resultData = new ResultData(integer);
        return resultData;
    }

    /**
     * 修改：  http://localhost:8080/dept/updateDeptByNotNull
     * {"deptno":1,"dname":"IOS开发部","loc":"长沙市"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept/modifyDeptByNotNull")
    public ResultData modifyDeptByNotNull(@RequestBody Dept dept) {
        Integer integer = deptMapper.updateDeptByNotNull(dept);
        ResultData resultData = new ResultData(integer);
        return resultData;
    }

    /**
     * 根据主键删除
     * http://localhost:8080/dept/del/4
     *
     * @param deptno
     * @return
     */
    @GetMapping("/dept/del/{deptno}")
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Object> delDeptById(@PathVariable("deptno") Integer deptno) {
        Integer integer = deptMapper.deleteDeptById(deptno);
        ResultData<Object> resultData = new ResultData<>(integer);
        return resultData;
    }

}
